package com.doangeometrydash.scene;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import com.doangeometrydash.base.BaseScene;
import com.doangeometrydash.manager.SceneManager;
import com.doangeometrydash.manager.SceneManager.SceneType;

public class GameOverScene extends BaseScene implements IOnAreaTouchListener
{
	int angle;
	Sprite gameOver;
	Sprite playAgain;

	@Override
	public void createScene()
	{
		gameOver = new Sprite(mCamera.getCameraSceneWidth() / 2, mCamera.getCameraSceneHeight() / 2, mResourcesManager.game_over_region, vbom);
		playAgain = new Sprite(mCamera.getCameraSceneWidth() - mCamera.getCameraSceneWidth() / 10, mCamera.getCameraSceneHeight() / 5 - 15,
				mResourcesManager.play_again_region, vbom);

		this.attachChild(gameOver);

		this.attachChild(playAgain);
		this.setOnAreaTouchListener(this);
		this.registerTouchArea(playAgain);
		angle = 0;
		mEngine.registerUpdateHandler(new TimerHandler(1 / 30f, true, new ITimerCallback()
		{

			@Override
			public void onTimePassed(TimerHandler pTimerHandler)
			{
				if (angle >= 360)
				{
					angle = 0;
				}
				else
				{
					playAgain.setRotation(angle);
					angle += 3;
				}

			}
		}));
	}

	@Override
	public void onBackKeyPressed()
	{
		this.disposeScene();
		SceneManager.getInstance().loadMenuScene();
	}

	@Override
	public SceneType getSceneType()
	{
		return SceneManager.SceneType.SCENE_GAMEOVER;
	}

	@Override
	public void disposeScene()
	{
		gameOver.dispose();
		gameOver.detachSelf();
		playAgain.dispose();
		playAgain.detachSelf();
		this.dispose();
		this.detachSelf();
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if (pSceneTouchEvent.isActionDown())
		{
			if (pTouchArea.contains(playAgain.getX(), playAgain.getY()))
			{
				playAgain.setScale(2f);
				SceneManager.getInstance().loadGameScene();
				this.disposeScene();
			}
		}
		return false;
	}

}
